package com.example.playbox

import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.playbox.utils.MD5Utils

class RegisterActivity : AppCompatActivity() {
    // 声明所有按钮
    private lateinit var etUsername: EditText
    private lateinit var etPwd: EditText
    private lateinit var etPwdSure: EditText
    private lateinit var register: Button
    private var userName: String? = null
    private var passWord: String? = null
    private var passWordSure: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        init()
    }

    private fun init() {
        // 给按钮匹配id
        etUsername = findViewById(R.id.username)
        etPwd = findViewById(R.id.pwd)
        etPwdSure = findViewById(R.id.pwd2)
        register = findViewById(R.id.registerBtn)
        register.setOnClickListener {
            getEditString()
            // 提示输入信息
            when {
                TextUtils.isEmpty(userName) -> {
                    Toast.makeText(this@RegisterActivity, "请输入用户名", Toast.LENGTH_SHORT).show()
                }
                TextUtils.isEmpty(passWord) -> {
                    Toast.makeText(this@RegisterActivity, "请输入密码", Toast.LENGTH_SHORT).show()
                }
                TextUtils.isEmpty(passWordSure) -> {
                    Toast.makeText(this@RegisterActivity, "请再次输入密码", Toast.LENGTH_SHORT).show()
                }
                passWord != passWordSure -> {
                    Toast.makeText(this@RegisterActivity, "输入两次的密码不一样", Toast.LENGTH_SHORT).show()
                }
                isExistUserName(userName!!) -> {
                    Toast.makeText(this@RegisterActivity, "此账户名已经存在", Toast.LENGTH_SHORT).show()
                }
                else -> {
                    Toast.makeText(this@RegisterActivity, "注册成功", Toast.LENGTH_SHORT).show()
                    // 保存账号密码
                    saveRegisterInfo(userName!!, passWord!!)
                    val data = Intent()
                    data.putExtra("userName", userName)
                    setResult(RESULT_OK, data)
                    // 跳转到登录界面中
                    val intent = Intent(this@RegisterActivity, LoginActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            }
        }
    }

    // 获得已输入信息
    private fun getEditString() {
        userName = etUsername.text.toString().trim()
        passWord = etPwd.text.toString().trim()
        passWordSure = etPwdSure.text.toString().trim()
    }

    // 判断输入的用户名是否已经存在
    private fun isExistUserName(userName: String): Boolean {
        val sp = getSharedPreferences("loginInfo", MODE_PRIVATE)
        val spPsw = sp.getString(userName, "")
        // 判断密码是否为空，不空则注册成功
        return !TextUtils.isEmpty(spPsw)
    }

    // 将用户名和密码保存到sp中
    private fun saveRegisterInfo(userName: String, psw: String) {
        val md5Psw = MD5Utils.md5(psw) // 把密码用MD5加密
        val sp = getSharedPreferences("loginInfo", MODE_PRIVATE)
        val editor = sp.edit()
        editor.putString(userName, md5Psw)
        editor.commit()
    }

    // 跳转回登录界面
    override fun onBackPressed() {
        super.onBackPressed()
        val intent = Intent(this@RegisterActivity, LoginActivity::class.java)
        startActivity(intent)
        finish()
    }
}